﻿package  
{
    import com.adamatomic.flixel.FlxButton;
    import com.adamatomic.flixel.FlxSprite;
    import com.adamatomic.flixel.FlxState;
    import com.adamatomic.flixel.FlxText;

    // A basic popup for giving information to the user.
    //
    public class Popup
    {
        // Image data used by this class
        //
		[Embed(source = "../data/popup.png")] private var mImgBG:Class;
        [Embed(source = "../data/popup_closebutton.png")] private var mImgClose:Class;
        [Embed(source = "../data/popup_closebutton_selected.png")] private var mImgCloseSelected:Class;
        
        // The sprite which shows what the current LOD is.
		//
		private var mBGDisplay:FlxSprite;
        
		// The object we use to display the tile.  
		//
		public var mText:FlxText;
		public var mTextShadow:FlxText;
        
        // The button used to close the popup
        //
        public var mCloseButton:FlxButton
        
        // This function will be called when the popup is closed, giving
        // clients a chance to do something at that time.
        //
        public var mCallback:Function;
        
        // Constructor
        //
        public function Popup( state:FlxState ) 
        {
            // Create all the GUI objects.
            //
            mBGDisplay      = state.add( new FlxSprite( mImgBG ) ) as FlxSprite;
            mTextShadow     = state.add( new FlxText( 78, 110, 172, 40, "", 0x333333, null, 16, "center" ) ) as FlxText;
            mText           = state.add( new FlxText( 76, 108, 172, 40, "", 0xcfe7e7, null, 16, "center" ) ) as FlxText;
            mCloseButton    = state.add( new FlxButton( 243, 103, new FlxSprite( mImgClose ), OnClosePressed, new FlxSprite( mImgCloseSelected ) ) ) as FlxButton;
            
            // The popup is hidden by default.
            Hide( );
        }
        
        // Functionality for showing the popup.  The msg will be displayed
        // and callback will be called when the popup is closed.
        //
        public function Show( msg:String, callback:Function ) : void
        {   
            mText.setText( msg );
            mTextShadow.setText( msg );
            mCloseButton.active = true;
            
            mBGDisplay.visible      = true;
            mTextShadow.visible     = true;
            mText.visible           = true;
            mCloseButton.visible    = true;
            
            mCallback = callback;
        }
        
        // Hides the popup.  This will call the callback function supplied when Show
        // was called.
        //
        public function Hide( ) : void
        {
            mBGDisplay.visible      = false;
            mTextShadow.visible     = false;
            mText.visible           = false;
            mCloseButton.visible    = false;
            
            mCloseButton.active     = false;
        }
        
        // Callback for the close button.
        //
        public function OnClosePressed( ) : void
        {
            Hide( );
            
            mCallback( );
        }
        
    }

}